package com.yx.modules.api.service.impl;

import com.yx.modules.api.dao.TbChannelDao;
import com.yx.modules.api.entity.TbChannel;
import com.yx.modules.api.service.TbChannelService;
import com.yx.system.dao.SysUserDao;
import com.yx.system.dao.SysUserRoleDao;
import com.yx.system.entity.SysUser;
import com.yx.system.entity.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;




@Service
public class TbChannelServiceImpl implements TbChannelService {
	@Autowired
	private TbChannelDao channelDao;

	@Autowired
	private SysUserDao sysUserDao;

	@Autowired
	private SysUserRoleDao sysUserRoleDao;
	
	@Override
	public TbChannel queryObject(Integer id){
		return channelDao.queryObject(id);
	}
	
	@Override
	public List<TbChannel> queryList(Map<String, Object> map){
		return channelDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return channelDao.queryTotal(map);
	}
	
	@Override
	public void save(TbChannel channel){
		channelDao.save(channel);
	}
	
	@Override
	public void update(TbChannel channel){
		channelDao.update(channel);
	}
	
	@Override
	public void delete(Integer id){
		channelDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		channelDao.deleteBatch(ids);
	}

	@Override
	public void deleteChannel(Integer[] ids) {
		for (int i = 0; i < ids.length; i++) {
			//根据id查找渠道对象
			TbChannel tbChannel = channelDao.queryObject(ids[i]);
			if (tbChannel!=null){
				//查询对应的后台用户
				SysUser sysUser = sysUserDao.queryByUserName(tbChannel.getCUser());
				if (sysUser!=null){
					//删除对应的用户角色中间表
					sysUserRoleDao.deleteByUserId(sysUser.getUserId());
					//删除用户
					sysUserDao.deleteByUserId(sysUser.getUserId());
				}
				//删除这个渠道
				channelDao.delete(tbChannel.getId());
			}
		}
	}

}
